<template>
  <div class="scenic-info">
    <!-- 操作按钮 -->
    <div class="action-buttons">
      <a-space>
        <a-button v-if="!isEditing" type="primary" @click="toggleEdit" :icon="h(EditOutlined)">
          编辑景点
        </a-button>
        <a-button danger @click="handleDelete" :loading="deleteLoading" :icon="h(DeleteOutlined)">
          删除景点
        </a-button>
      </a-space>
    </div>

    <!-- 编辑模式 - 使用独立的编辑组件 -->
    <UpdateScenicForm
      v-if="isEditing"
      :scenic-data="scenicData"
      @updated="handleScenicUpdated"
      @refresh="handleScenicRefresh"
      @cancel="handleEditCancel"
    />

    <!-- 查看模式 -->
    <div v-else>
      <!-- 景点基本信息 -->
      <div class="scenic-header">
        <div class="cover-image">
          <img :src="scenicData.coverImage" :alt="scenicData.scenicName" />
        </div>
        <div class="basic-info">
          <h2 class="scenic-name">{{ scenicData.scenicName }}</h2>
          <div class="tags">
            <a-tag
              v-for="tag in scenicData.tags || []"
              :key="tag"
              color="blue"
              style="margin-bottom: 8px"
            >
              {{ tag }}
            </a-tag>
          </div>
          <div class="location">
            <EnvironmentOutlined style="margin-right: 8px; color: #1890ff" />
            <span>经度: {{ scenicData.scenicLongitude || '未设置' }}°</span>
            <a-divider type="vertical" />
            <span>纬度: {{ scenicData.scenicLatitude || '未设置' }}°</span>
          </div>
          <div class="time-info">
            <ClockCircleOutlined style="margin-right: 8px; color: #52c41a" />
            <span>创建时间: {{ formatTime(scenicData.createTime || '') }}</span>
          </div>
        </div>
      </div>

      <!-- 景点简介 -->
      <a-divider orientation="left">
        <FileTextOutlined style="margin-right: 8px" />
        景点简介
      </a-divider>
      <div class="scenic-brief">
        <p>{{ scenicData.scenicBrief || '暂无简介' }}</p>
      </div>

      <!-- 游玩建议 -->
      <a-divider orientation="left">
        <BulbOutlined style="margin-right: 8px" />
        游玩建议
      </a-divider>
      <div class="scenic-tips">
        <a-alert
          :message="scenicData.tips || '暂无游玩建议'"
          type="info"
          show-icon
          style="margin-bottom: 16px"
        />
      </div>

      <!-- 景点图片 -->
      <a-divider orientation="left">
        <PictureOutlined style="margin-right: 8px" />
        景点图片
      </a-divider>
      <div class="scenic-images">
        <div
          v-if="scenicData.scenicImages && scenicData.scenicImages.length > 0"
          class="image-gallery"
        >
          <a-image-preview-group>
            <a-image
              v-for="(image, index) in scenicData.scenicImages"
              :key="index"
              :src="image"
              :alt="`${scenicData.scenicName}-图片${index + 1}`"
              class="image-item"
              :preview="{
                mask: h('div', { class: 'image-overlay' }, [h(EyeOutlined)]),
              }"
            />
          </a-image-preview-group>
        </div>
        <div v-else class="no-images">
          <a-empty description="暂无景点图片" />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, h } from 'vue'
import { message, Modal } from 'ant-design-vue'
import {
  EnvironmentOutlined,
  ClockCircleOutlined,
  FileTextOutlined,
  BulbOutlined,
  PictureOutlined,
  EyeOutlined,
  EditOutlined,
  DeleteOutlined,
  ExclamationCircleOutlined,
} from '@ant-design/icons-vue'
import UpdateScenicForm from './UpdateScenicForm.vue'
import { deleteScenic } from '@/api/scenicController'

// 定义 props 和 emits
interface Props {
  scenicData: API.ScenicVO
}

const props = defineProps<Props>()

const emit = defineEmits<{
  updated: [scenicData: API.ScenicVO]
  refresh: [scenicId: string] // 新增：请求刷新景点数据
  deleted: [scenicId: number] // 新增：景点删除事件
}>()

const isEditing = ref(false)
const deleteLoading = ref(false)

// 格式化时间
const formatTime = (timeString: string) => {
  if (!timeString) return '未知'
  const date = new Date(timeString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
  })
}

// 切换编辑模式
const toggleEdit = () => {
  isEditing.value = true
}

// 处理景点更新成功
const handleScenicUpdated = (updatedScenicData: API.ScenicVO) => {
  isEditing.value = false
  emit('updated', updatedScenicData)
}

// 处理景点刷新请求
const handleScenicRefresh = (scenicId: string) => {
  isEditing.value = false
  console.log('🔄 ScenicInfo 收到刷新请求，景点ID:', scenicId)
  // 通知父组件刷新景点数据
  emit('refresh', scenicId)
}

// 处理取消编辑
const handleEditCancel = () => {
  isEditing.value = false
}

// 处理删除景点
const handleDelete = () => {
  if (!props.scenicData.id) {
    message.error('景点ID无效，无法删除')
    return
  }

  Modal.confirm({
    title: '确认删除',
    icon: h(ExclamationCircleOutlined),
    content: `确定要删除景点"${props.scenicData.scenicName}"吗？此操作不可恢复。`,
    okText: '确定删除',
    okType: 'danger',
    cancelText: '取消',
    onOk: async () => {
      try {
        deleteLoading.value = true
        const response = await deleteScenic({ id: props.scenicData.id! })

        if (response.data.code === 200) {
          message.success('景点删除成功！')
          emit('deleted', props.scenicData.id!)
        } else {
          message.error('删除失败：' + (response.data.message || '未知错误'))
        }
      } catch (error) {
        console.error('删除景点失败:', error)
        message.error('删除失败，请稍后再试')
      } finally {
        deleteLoading.value = false
      }
    },
  })
}

// 防止 props 未使用警告（props 在模板中使用）
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _ = props
</script>

<style scoped>
.scenic-info {
  padding: 24px;
  max-height: 70vh;
  overflow-y: auto;
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.scenic-header {
  display: flex;
  gap: 24px;
  margin-bottom: 24px;
}

.cover-image {
  flex-shrink: 0;
  width: 200px;
  height: 150px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.cover-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.basic-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.scenic-name {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
  color: #1890ff;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.location,
.time-info {
  display: flex;
  align-items: center;
  color: #666;
  font-size: 14px;
}

.scenic-brief {
  background: #f5f5f5;
  padding: 16px;
  border-radius: 8px;
  border-left: 4px solid #1890ff;
}

.scenic-brief p {
  margin: 0;
  line-height: 1.8;
  color: #333;
  text-align: justify;
  text-indent: 2em;
}

.scenic-tips {
  margin-bottom: 16px;
}

.image-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}

.image-gallery .ant-image {
  width: 100%;
  height: 120px;
  border-radius: 8px;
  overflow: hidden;
}

.image-gallery .ant-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.2s;
}

.image-gallery .ant-image:hover img {
  transform: scale(1.02);
}

.image-overlay {
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  background: rgba(0, 0, 0, 0.5);
}

.no-images {
  padding: 20px;
  text-align: center;
  background: #fafafa;
  border-radius: 8px;
  border: 1px dashed #d9d9d9;
}

.image-upload-container {
  width: 100%;
}

.current-cover-image {
  margin-bottom: 16px;
  text-align: center;
}

.current-images {
  margin-bottom: 16px;
}

.current-images-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}

.current-image-item {
  position: relative;
  width: 120px;
  height: 90px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #d9d9d9;
}

.current-image-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.delete-image-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.delete-image-btn:hover {
  background: rgba(255, 255, 255, 1);
  color: #ff4d4f;
}

/* 滚动条样�?*/
.scenic-info::-webkit-scrollbar {
  width: 6px;
}

.scenic-info::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.scenic-info::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.scenic-info::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 响应式设�?*/
@media (max-width: 768px) {
  .scenic-header {
    flex-direction: column;
  }

  .cover-image {
    width: 100%;
    height: 200px;
  }

  .image-gallery {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  }

  .image-gallery .ant-image {
    height: 80px;
  }

  .action-buttons {
    flex-direction: column;
    gap: 8px;
  }
}
</style>
